package com.somnus.demo.androiddatabingdemo;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.somnus.demo.androiddatabingdemo.adapter.RecyclerViewBaseAdapter;
import com.somnus.demo.androiddatabingdemo.bean.UserBean;
import com.somnus.demo.androiddatabingdemo.databinding.ActivityMainBinding;
import com.somnus.demo.androiddatabingdemo.listener.MyHandlers;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding binding;
    private RecyclerViewBaseAdapter mRecyclerViewAdapter;

    UserBean mUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        mUser = new UserBean();
        mUser.setUserName("张三");
        mUser.setUserAge("28岁");
        mUser.setUserIndex("zhang");
        mUser.setUserAdult(true);
        mUser.setUserIcon("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1493984139034&di=9641134be6ab7765d2eb2917b3d65800&imgtype=0&src=http%3A%2F%2Fimg.qqzhi.com%2Fupload%2Fimg_0_2964044197D3255401337_23.jpg");
        binding.setUser(mUser);

        /*****点击事件***/
        binding.idTest.setText("id 点击 测试（单点击事件——）");
        binding.setHandlers(new MyHandlers()); //onclick 需要


        binding.setActivity(this);


        mRecyclerViewAdapter = new RecyclerViewBaseAdapter();
        //在MainActivity中，根据RecyclerView的id，会自动生成一个recyclerView。,我们可以不用findViewById，直接使用binding.recyclerView。
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerView.setAdapter(mRecyclerViewAdapter);
        mRecyclerViewAdapter.addData(genData());
    }


    private List<UserBean> genData() {
        ArrayList<UserBean> list = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            String name = "Somnus";
            int price = random.nextInt(10) + 10;
            int len = random.nextInt(80) + 60;
            UserBean movie = new UserBean();
            movie.setUserAge(len + "");
            movie.setUserName(name + i);
            list.add(movie);
        }
        return list;
    }

    public void onClickChange(View v) {
//        mUser.setUserName("李四（改）");
//        binding.setUser(mUser);

        /***
         * 继承 BaseObservable 來改變UI
         */
        startActivity(new Intent(this, BaseObservaleAty.class));


    }


}
